﻿using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary ;
namespace QiangPiaoTool
{
    public class Serializer
    {
        /// <summary>
        /// 序列化（二进制）
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        public static bool Serialize(string path, object data)
        {
            try
            {
                FileStream fs = new FileStream(@path, FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, data);
                fs.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 反序列化（二进制）
        /// </summary>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object Deserialize(string path)
        {
            try
            {
                FileStream fs = new FileStream(@path, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                object objRes=bf.Deserialize(fs);
                fs.Close();
                return objRes;
            }
            catch
            {
                return null;
            }
        }

    }
}
